﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.Utility;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.DataDictionary.Agents;
using Sector7.Framework.Enums;
using Sector7.Framework.Consts;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// AddValueTypeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddValueTypeWindow : AddModeWindowBase
    {
        public S7_ValueType ValueType
        {
            get { return m_entity as S7_ValueType; }
            set { m_entity = value; }
        }

        public AddValueTypeWindow()
        {
            InitializeComponent();

            FocusedControl = txbName;
        }

        protected override void AddData()
        {
            ValueType.TypeName = txbName.Text;
            ValueType.TypeIdentifier = txbIdentifier.Text;
            ValueType.TypeDescription = txbDescription.Text;

            switch (m_crud)
            {
                case CrudType.Create:
                    ValueType.TypeId = ValueTypeServiceAgent.Add(ValueType);
                    break;
                case CrudType.Update:
                    bool isChanged = false;
                    EntityDeltaBase delta = GetDelta(C_PrimaryKeyName.VALUE_TYPE, ref isChanged);

                    if (isChanged)
                    {
                        ValueTypeServiceAgent.Set(delta);
                    }
                    break;
                default:
                    break;
            }

            base.AddData();
        }

        protected override void InitializeForCreating()
        {
            txbName.Text = string.Empty;
            txbIdentifier.Text = string.Empty;
            txbDescription.Text = string.Empty;

            m_entity = new S7_ValueType();
        }

        protected override void InitializeForUpdating()
        {
            txbName.Text = ValueType.TypeName;
            txbIdentifier.Text = ValueType.TypeIdentifier;
            txbDescription.Text = ValueType.TypeDescription;

            btnContinue.Visibility = Visibility.Hidden;
        }
    }
}
